# @Time:2021/6/10 10:34
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import pygame
from pygame.constants import QUIT
from scene.tiled_scene import TileScene as tilescene
from actor.swk import SWK
from actor.earth_god import EarthGod
from actor.elder3 import Elder3
from actor.elder2 import Elder2
from actor.elder1 import Elder1
from actor.elder4 import Elder4
import pytmx
from pygame.locals import *
from scene.fade_scene import FadeScene, SceneStatus
from scene import SceneResult


# 对村庄类进行封装
class VillageScene(object):
    scene_result = SceneResult.Ongoing  # 表示这个场景正在运行

    def __init__(self, swk: SWK, surface: pygame.Surface):  # 第二个参数是主图层
        self.screen = surface
        backtmxpath = os.path.join('../resource', 'tmx', 'village1.tmx')
        # 加载.tmx图片
        self.tiled_scene = tilescene(backtmxpath)
        self.back_surface = self.tiled_scene.surface
        self.fade_scene = FadeScene(self.back_surface)
        self.temp_surface = pygame.Surface((800, 600))
        self.swk_player = swk
        self.obstacle_group = pygame.sprite.Group()  # 创建障碍物的精灵组
        # 窗口位置的初始化
        self.win_pos_x = 0
        self.win_pos_y = 0
        self.dx_dy = [0, 0]  # 这个值等于孙悟空移动的值
        self.init_actor()

        # 添加音效
        sound_path = os.path.join('../resource', 'sound', 'aigei.mp3')
        pygame.mixer.music.load(sound_path)
        pygame.mixer.music.play(-1)  # 循环播放

    # 因为加载对象层很复杂 所以单独封装一个函数
    def init_actor(self):
        # 孙悟空的位置加载进来之后 可以算出孙悟空相对于整个图片的位置 然后调用函数计算出孙悟空相对于窗体的位置
        # 取出瓦格图片有关的数据
        for group in self.tiled_scene.tiled.tmx_data.objectgroups:
            if isinstance(group, pytmx.pytmx.TiledObjectGroup):  # 判断是不是瓦格的对象组
                if group.name == 'god':
                    for obj in group:
                        self.earth_god = EarthGod(obj.x, obj.y)  # 传入相对于.tmx图片的位置
                if group.name == 'elder':
                    for obj in group:
                        if obj.name == 'elder3':
                            self.elder3 = Elder3(obj.x, obj.y)
                        if obj.name == 'elder2':
                            self.elder2 = Elder2(obj.x, obj.y)
                        if obj.name == 'elder1':
                            self.elder1 = Elder1(obj.x, obj.y)
                        if obj.name == 'elder4':
                            self.elder4 = Elder4(obj.x, obj.y)
                if group.name == 'actor':  # 判断是不是actor对象组
                    for obj in group:  # 遍历actor对象组
                        if obj.name == 'sun':  # 判断是否有孙悟空
                            self.swk_player.set_pos(obj.x, obj.y)  # 将孙悟空全局的位置赋值给pos_x和pos_y
                            self.win_pos_x = obj.x - 400  # 将孙悟空显示在中间   窗口的大小是(800,600)
                            self.win_pos_y = obj.y - 300
                if group.name == 'obstacle':  # 判断是不是障碍物的对象组
                    for obj in group:
                        obs = pygame.sprite.Sprite()  # 为每一个障碍物都创建一个精灵对象
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)  # 加入到障碍物的精灵组

    # 得到当前窗体的图层 并进行绘制
    def get_curr_surface(self):
        # self.win_surface = self.back_surface.subsurface(self.win_pos_x, self.win_pos_y, 800, 600)  # 每次都要重新获取win_surface
        self.win_surface = self.fade_scene.get_back_image(self.win_pos_x, self.win_pos_y)  # 获取渐变场景
        self.temp_surface.blit(self.win_surface, (0, 0))  # 绑定
        self.swk_player.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)  # 孙悟空画在temp_surface上
        self.earth_god.draw(self.temp_surface, self.win_pos_x, self.win_pos_y,True)  # 土地公画在temp_surface上
        self.elder3.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)  # elder3画在temp_surface上
        self.elder2.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        self.elder1.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        self.elder4.draw(self.temp_surface,self.win_pos_x,self.win_pos_y)
        self.screen.blit(self.temp_surface, (0, 0))

    # 循环体
    def run(self):
        scene_exit = False
        clock = pygame.time.Clock()
        # 主图层
        while not scene_exit:
            key_down = False
            self.dx_dy = [0, 0]
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.scene_result = SceneResult.Quit
                    scene_exit = True
                if event.type == KEYDOWN:
                    key_down = True
                    pressed_key = event.key
                    self.dx_dy = self.swk_player.key_move(pressed_key, key_down, self.obstacle_group)
                    if key_down == True:
                        self.win_pos_x += self.dx_dy[0]
                        self.win_pos_y += self.dx_dy[1]
                        if self.earth_god.is_stop and pressed_key == K_y:  # 当土地公是停止的&&按下了y键
                            # 进入temple场景
                            self.fade_scene.set_status(SceneStatus.Out)
                            self.earth_god.is_stop = False  # 重置土地公
            if self.fade_scene.get_out():  # 判断渐出的效果是否执行完了
                self.scene_result = SceneResult.Next
                scene_exit = True

            # 越界判断
            if self.win_pos_x < 0:
                self.win_pos_x = 0
            if self.win_pos_y < 0:
                self.win_pos_y = 0
            if self.win_pos_x > (3780 - 800):
                self.win_pos_x = 3780 - 800
            if self.win_pos_y > (2395 - 600):
                self.win_pos_y = 2395 - 600
            if self.fade_scene.status != SceneStatus.Out:  # 当场景已经渐出的时候就不用判断了
                collide_result = self.earth_god.collide(self.swk_player)
                collide_result1 = self.elder3.collide(self.swk_player)
                collide_result2 = self.elder2.collide(self.swk_player)
                collide_result3 = self.elder1.collide(self.swk_player)
                collide_result4 = self.elder4.collide(self.swk_player)
            # if collide_result:  # 碰上了
            #     print("孙悟空碰上土地公了")
            # earth_god.Move(1,1)
            self.earth_god.Move()
            self.get_curr_surface()
            pygame.display.update()
            clock.tick(15)  # 设置每次循环的帧率 防止对象层的对象移动过快

    # def single_frame_operation(self, press_key: int, key_down: bool):  # 第二个参数是int类型的是否按键 第三个参数是bool类型的是否按下键
    #     collide_result = self.earth_god.collide(self.swk_player)
    #     if collide_result:  # 碰上了
    #         self.swk_player.is_stop = True
    #         print("孙悟空碰上了土地公")
    #     else:
    #         self.swk_player.is_stop = False
    #     if key_down == True:
    #         self.swk_player.key_move(press_key, key_down, self.obstacle_group)
    #         self.win_pos_x += self.dx_dy[0]
    #         self.win_pos_y += self.dx_dy[1]
    #     # 越界判断
    #     if self.win_pos_x < 0:
    #         self.win_pos_x = 0
    #     if self.win_pos_y < 0:
    #         self.win_pos_y = 0
    #     if self.win_pos_x > (3780 - 800):
    #         self.win_pos_x = 3780 - 800
    #     if self.win_pos_y > (2395 - 600):
    #         self.win_pos_y = 2395 - 600
    #     self.earth_god.Move()
